import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Table} from 'antd';
import {TABLE, DATE_FORMAT} from '../constants';
import {formatAmount, formatDateTime} from '../utils';
import routes from './routes';
import qs from 'qs';

export class InvoiceDetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        const order = this.getUrl(this.props.history.location.search);
        this.props.init({
            brandId: order.brandId,
            partId: order.partId,
            partSaleOrderId: order.partSaleOrderId,
        });
    }
    getUrl = str => {
        const r = qs.parse(str.replace('?', ''));
        if(r !== null)
            return r;
        return null;
    };
    render() {
        const detailData = this.props.detail || {};
        const {formatMessage} = this.props.intl;
        const columns = [{
            title: formatMessage({
                id: 'invoiceDetailPanel.column.partInvoiceCode',
                defaultMessage: '发票号'
            }),
            dataIndex: 'partInvoiceCode',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.partSaleOrderCode',
                defaultMessage: '订单号'
            }),
            dataIndex: 'partSaleOrderCode',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.partOrderTypeName',
                defaultMessage: '订单类型'
            }),
            dataIndex: 'partOrderTypeName',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.submitTime',
                defaultMessage: '提报日期'
            }),
            dataIndex: 'submitTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.creditAuditedTime',
                defaultMessage: '信用审核日期'
            }),
            dataIndex: 'creditAuditedTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.englishName',
                defaultMessage: '备件英文名称'
            }),
            dataIndex: 'englishName',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.price',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.minPackSize',
                defaultMessage: '最小包装数'
            }),
            dataIndex: 'minPackSize',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.quantity',
                defaultMessage: '数量'
            }),
            dataIndex: 'quantity',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.totalAmount',
                defaultMessage: '金额'
            }),
            dataIndex: 'totalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.oldPartCode',
                defaultMessage: '被替换件'
            }),
            dataIndex: 'oldPartCode',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.palletCode',
                defaultMessage: '托盘号'
            }),
            dataIndex: 'palletCode',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.perNetWeight',
                defaultMessage: '净重(KG)'
            }),
            dataIndex: 'perNetWeight',
        }, {
            title: formatMessage({
                id: 'invoiceDetailPanel.column.netWeight',
                defaultMessage: '总净重(KG)'
            }),
            dataIndex: 'netWeight',
        }];
        return (
            <div className="detail">
                <Card loading={this.props.loading} title={formatMessage({
                    id: 'invoiceDetailPanel.card.partInvoiceTrack',
                    defaultMessage: '备件出运跟踪详情'
                })}>
                    <Table
                        className="white-space-nowrap"
                        columns={columns}
                        rowKey="id"
                        pagination={false}
                        dataSource={detailData}
                        {...TABLE} />
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'invoiceDetailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

InvoiceDetailPanel.propTypes = {
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {getInvoiceDetail} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getDetailData = selectorFactory(['page', 'domainData', 'detailInvoice', 'data']);
const mapStateToProps = (state, props) => ({
    loading: state.getIn(['page', 'domainData', 'detailInvoice', 'isFetching']),
    detail: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: data => dispatch(getInvoiceDetail(data)),
    onCancel: () => {
        props.history.push(routes.query.url());
    }
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(InvoiceDetailPanel));
